package com.juno.cafeNoa.admin.adapter;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.juno.cafeNoa.admin.R;
import com.juno.cafeNoa.admin.data.Event;
import com.juno.cafeNoa.admin.listener.EventListCompelete;
import com.juno.cafeNoa.admin.net.AsyncTaskCompleteListener;
import com.juno.cafeNoa.admin.net.HttpPostTask;
import com.juno.cafeNoa.admin.net.NetConstant;
import com.juno.cafeNoa.admin.util.Util;

public class EventADP extends ArrayAdapter<Event> {
	private Context mContext;
	private int mResource;
	private ArrayList<Event> mList;
	private LayoutInflater mInflater;
	private int clickPosition;
	
	private EventListCompelete mListener;
	
	
	public EventADP(Context context, int layoutResource, ArrayList<Event> list) {
		super(context, layoutResource, list);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = list;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setListener(EventListCompelete listener){
		this.mListener = listener;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || convertView.getId() !=mResource) {
			convertView = mInflater.inflate(mResource, null);
			holder = new ViewHolder();
			holder.img = (ImageView) convertView.findViewById(R.id.eventImage);
			holder.content = (TextView) convertView.findViewById(R.id.event_content);
			holder.name = (TextView) convertView.findViewById(R.id.event_name);
			holder.startTime = (TextView) convertView.findViewById(R.id.event_start_time);
			holder.endTime = (TextView) convertView.findViewById(R.id.event_end_time);
			holder.endBtn = (Button) convertView.findViewById(R.id.event_end_btn);

			convertView.setTag(holder);
			convertView.setId(R.layout.row_event);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.content.setText(mList.get(position).getContent());
		holder.name.setText(mList.get(position).getName());
		holder.startTime.setText(String.valueOf(mList.get(position).getStartTime()));
		holder.endTime.setText(mList.get(position).getEndTime());
		
		holder.endBtn.setText("종료");
		holder.endBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clickPosition = position;
			
				AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
				ab.setTitle("확인");
				ab.setIcon(R.drawable.ic_launcher);
				ab.setMessage("이벤트를 종료하시겠습니까 ?");
				ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						eventEnd.excuteHttpTask();
					}
				});

				ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});

				AlertDialog alert = ab.create();
				alert.show();
			}
			
		});
		return convertView;
	}

	class ViewHolder {
		ImageView img;
		TextView state;
		TextView name;
		TextView content;
		TextView startTime;
		TextView endTime;
		
		Button endBtn;
	}
	
	AsyncTaskCompleteListener eventEnd= new AsyncTaskCompleteListener() {

		@Override
		public void excuteHttpTask() {
			String url = Util.getUrl("EventDelete");
			
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			value.add(new BasicNameValuePair("EVENT_ID", String.valueOf(mList.get(clickPosition).getId())));
			
			HttpPostTask http = new HttpPostTask(mContext, value, url, eventEnd);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			Log.v("result","reuslt :" + result);
			if(taskOk){
				int code = -1;
				
				try {
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");

					if(code == NetConstant.NETWORK_SUCCESS){
						Toast.makeText(mContext, "정상적으로 처리되었습니다.", Toast.LENGTH_LONG).show();
						mListener.EventComplete(true, clickPosition);
					} else {
						Toast.makeText(mContext, "서버오류 !.", Toast.LENGTH_LONG).show();
						mListener.EventComplete(false, -1);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	};
}
